## @package libsigma
#  Designer-facing utility functions.

import traceback, sys#, command
from string import Template
#from common import *

## Run a function within an error-trapped environment.
#
#  The safe_mode function is used to trap errors in non-essential
#  (usually designer-generated) code, to avoid a forced quit and stack
#  trace in normal game operation.
#
#  @param function The function to execute.
#  @param args The arguments to pass to the function.
def safe_mode(function, *args):
	ret = False

	try:
		ret = function(*args)
	except:
		tb = sys.exc_info()[2]
		last = traceback.extract_tb(tb)[-1]
		log("  *  ERROR", last[0] + ":" + str(last[1]) + " (" + last[2] + ")")

	return ret

## Provide a basic means of producing log text within designer code.
#
#  @param text The text to include in the log entry.
def alert(text):
	log("  *  ALERT", text)


#def queue_command(character, text):
	#command.accept_command(character, text)

## Force a simulated command to be run immediately.
#
#  @param character The character that receives the simulated-typed command.
#  @param text The text to simulate-type.
#
#  @sa Use queue_command to queue the command, rather than executing it immediately.
def run_command(character, text):
	if not command.run_command(character, text):
		log("  *  ERROR", "Command <" + text + "> unsuccessful")

